<?php
class TribeControl extends BaseControl {

	private $__dal_community;
	private $__dal_community_member;
	
	public function __construct() {
		parent::__construct();
	    $remotecall = new remotecall('');
  	 	!$remotecall->check_sign() && $this->error('验证不成功');
  	 	$this->__dal_community = m('community');
  	 	$this->__dal_community_member = m('community_member');
	}
	
	/**
	 * 检测社区
	 * @param string $names
	 * @return string|array, 如果匹配不到返回社区的名称数组，否则返回社区IDS
	 */
	
	public function checkFriend() {	
		
		$ids = '';
		$arr = array();
				
		$names = explode(';', $_REQUEST['names']);
		
		if(is_array($names)){
			foreach ($names as $key => $name) {
				
				$condition = array();
				$condition['name'] = "= '" . $name . "'";
		
				$friendInfo = $this->__dal_community->sampleFindFriendInfo($condition, '', 1);

				if (empty($friendInfo)) {
					$arr[] = $name;
					
				} else {
					$ids .= $friendInfo['cmtyid'] . ',';
				}											
			}
		}
		
		if (!empty($arr)) {
			$this->jsonmsg('', $arr);
		} else {
			$this->jsonmsg('', rtrim($ids, ','));
		}
	}
	
	/**
	 * 获取用户加入的社区IDS
	 */
	
	public function getUserJoinCommunityIds() {
	    
	    $communityIds = array();
	    
	    $condition = array();
		$condition['uid'] = "= " . $_SESSION['user_info']['id'];
	    $communityMemberInfoList = $this->__dal_community_member->sampleFindCommunityMemberInfo($condition);
	    
	    if (!empty($communityMemberInfoList)) {
	        foreach ($communityMemberInfoList as $key => $communityMemberInfo) {
	            $communityIds = $communityMemberInfo['cmtyid'];
	        }
	    }
	    
	    $this->jsonmsg('', $communityIds);
	}
	
	private function jsonmsg($msg,$datas=array()){
  	 	$fan['state'] = 1;
  	 	$fan['datas'] = $datas;
  	 	$fan['info'] = $msg;
  	    echo json_encode($fan);exit();
  	}
  	
  	private function error($msg){
  	    empty($msg) && $msg = '验证失败';
  	 	$fan['state'] = -1;
  		$fan['info'] = $msg;
  		echo json_encode($fan);exit();
  	}	 
}